package com.biz.primus.base.redis.util;

import com.biz.primus.base.redis.spi.ShardedJedisRedis;
import com.google.common.collect.Sets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.ShardedJedis;

import java.util.*;


/**
 * 用于操作简单数据的redis-dao,例如序列号池,token等
 * @author yanweijin
 * @since 2016年8月20日
 * @usage 
 * @reviewer
 */
public class SimpleDataRedisDao extends ShardedJedisRedis {
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	protected String getset(String key, String newVal) {
		ShardedJedis jedis = null;
		try {
			jedis = shardedJedisPool.getResource();
			return jedis.getSet(key,newVal);
		} finally {
			shardedJedisPool.returnResource(jedis);
		}
	}
	protected List<Long> bytesSet2LongList(Collection<byte[]> set){
		if(set == null){
			return Collections.emptyList();
		}
		List<Long> result = new ArrayList<>(set.size());
		for (byte[] bs : set) {
			result.add(RedisUtil.byteArrayToLong(bs));
		}
		return result;
	}
	protected Set<String> bytesSet2StringSet(Collection<byte[]> set){
		if(set == null){
			return Collections.emptySet();
		}
		Set<String> result = Sets.newHashSet();
		for (byte[] bs : set) {
			result.add(RedisUtil.byteArrayToStr(bs));
		}
		return result;
	}

	protected Long saddBatch(String key, String...members){
		try(ShardedJedis jedis = shardedJedisPool.getResource()){
			return jedis.sadd(key, members);
		}
	}

	protected Long sremBatch(String key, String...members){
		try(ShardedJedis jedis = shardedJedisPool.getResource()){
			return jedis.srem(key, members);
		}
	}

}
